package com.zhanghp.class035;

import java.io.*;
import java.util.HashMap;
import java.util.Map;

/**
 * setAll功能的哈希表<br/>
 * 测试链接 : https://www.nowcoder.com/practice/7c4559f138e74ceb9ba57d76fd169967<br/>
 * 请同学们务必参考如下代码中关于输入、输出的处理<br/>
 * 这是输入输出处理效率很高的写法<br/>
 * 提交以下的code，提交时请把类名改成"Main"，可以直接通过<br/>
 *
 * @author zhanghp
 * @since 2024/4/11 9:56
 */
public class Code01_SetAllHashMap {


    public static Map<Integer, int[]> map = new HashMap<>();

    /**
     * setAll值
     */
    public static int setAllVal;

    /**
     * setAll时间计次
     */
    public static int setAllTime;

    /**
     * 时间计次（用来看当前key，是否在setAll时间计次里的，返回value值还是setAll值）
     */
    public static int cnt;

    public static void init() {
        map.clear();
        setAllVal = -1;
        setAllTime = -1;
        cnt = 0;

    }

    public static void put(int key, int val) {
        // 覆盖相同key的value
        if (containsKey(key)) {
            int[] value = map.get(key);
            value[0] = val;
            value[1] = cnt++;
        } else {
            map.put(key, new int[]{val, cnt++});
        }
    }

    public static void setAll(int v) {
        setAllVal = v;
        // cnt ++原因：如果两次setAll,中间有put，那cnt只记录第一次setAll之前的
        setAllTime = cnt++;
    }

    public static int get(int key) {
        // 判断是否包含
        if (!containsKey(key)) {
            return -1;
        }
        // 判断是否在setAllTime计次内
        int[] ints = map.get(key);
        if (ints[1] > setAllTime) {
            return ints[0];
        } else {
            return setAllVal;
        }
    }

    public static boolean containsKey(int key) {
        return map.containsKey(key);
    }

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StreamTokenizer in = new StreamTokenizer(br);
        PrintWriter out = new PrintWriter(System.out);
        while (in.nextToken() != StreamTokenizer.TT_EOF) {
            init();
            // 需把循环的次数提出来
            int circleSize = (int) in.nval;
            for (int i = 0; i < circleSize; i++) {
                in.nextToken();
                int opt = (int) in.nval;
                switch (opt) {
                    // 存
                    case 1:
                        // 获取输入的数值
                        in.nextToken();
                        int key = (int) in.nval;
                        in.nextToken();
                        int val = (int) in.nval;
                        put(key, val);
                        break;
                    // 获取
                    case 2:
                        // 获取输入的数值
                        in.nextToken();
                        int containsKey = (int) in.nval;
                        int containsVal = get(containsKey);
                        // 结果输送到printWriter流里
                        out.println(containsVal);
                        break;
                    // 设当前map全部的val为指定数值
                    case 3:
                        // 获取输入的数值
                        in.nextToken();
                        int allVal = (int) in.nval;
                        setAll(allVal);
                        break;
                    default:
                        break;
                }
            }
        }

        out.flush();
        out.close();
        br.close();
    }

}
